name: Minor Version Release (1.x)

on:
  workflow_dispatch:

env:
  PYTHON_VERSION: "3.8"

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout this repo
        uses: actions/checkout@v4
        with:
          ref: "v1.x"

      - name: Define all versions
        id: versions
        shell: bash
        # We only need `major.minor` in Readme so we cut the full version string to the first two tokens
        run: |
          echo "current_release_minor=$(cut -d "." -f 1,2 < VERSION.txt)" >> "$GITHUB_OUTPUT"

      - name: Create new version branch
        # We tag the commit where we branch off as "<version>-rc0", so reno will know where to stop next
        # time we generate release notes for "next minor".
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git tag -m"v${{ steps.versions.outputs.current_release_minor }}.0-rc0" v${{ steps.versions.outputs.current_release_minor }}.0-rc0
          git checkout -b v${{ steps.versions.outputs.current_release_minor }}.x
          git push -u origin v${{ steps.versions.outputs.current_release_minor }}.x --tags

      - name: Bump version on v1.x
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git checkout v1.x
          NEW_VERSION=$(awk -F. '/[0-9]+\./{$2++;print}' OFS=. < VERSION.txt)
          echo "$NEW_VERSION" > VERSION.txt
          cat VERSION.txt
          git add .
          git commit -m "Update unstable version to $NEW_VERSION"
          VERSION_TAG="v$NEW_VERSION"
          git tag $VERSION_TAG -m"$VERSION_TAG"
          git push --atomic origin v1.x $VERSION_TAG

      - uses: actions/setup-python@v5
        with:
          python-version: "${{ env.PYTHON_VERSION }}"

      - name: Install release_docs.py dependencies
        run: pip install requests

      - name: Release Readme version
        env:
          RDME_API_KEY: ${{ secrets.README_API_KEY }}
        run: |
          git checkout v1.x
          python ./.github/utils/release_docs.py --new-version ${{ steps.versions.outputs.current_release_minor }}
